﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JCTimes.DomainModel.Entity;
using JCTimes.DomainModel.Repository;

namespace JCTimes.EFRepository.RepositoryImpl
{
    public class RoleRepository : BaseRepository<Role>, IRoleRepository {
        public IEnumerable<User> UsersByRole(string roleID) {
            return db.Users.Where(u => u.RoleID == roleID).ToList();
        }

        public IEnumerable<Function> FunctionsByRole(string roleID) {
            var query = from fr in db.FunctionRoles
                        from f in db.Functions
                        where fr.FunctionID == f.ID
                        select f;
            return query.Distinct();
        }

        /// <summary>
        /// Confirm whether the function is authorized to the role or not.
        /// </summary>
        /// <param name="roleID">role id</param>
        /// <param name="functionID">function id</param>
        /// <returns>true if the function has been authorized</returns>
        public bool IsMatch(string roleID, string functionID) {
            return db.FunctionRoles.Any(fr=>fr.FunctionID==functionID&&fr.RoleID==roleID);
        }
    }
}
